home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / obsolete / c_edit.pro < prev    next >
Text File  |  1997-07-08  |  10KB  |  334 lines

  1. ; $Id: c_edit.pro,v 1.2 1997/01/15 04:02:19 ali Exp $
  2. ;
  3. ; Copyright (c) 1988-1997, Research Systems, Inc.  All rights reserved.
  4. ;       Unauthorized reproduction prohibited.
  5.  
  6. pro draw_bar, bar
  7.  
  8. on_error,2                      ;Return to caller if an error occurs
  9. sx = bar.x1 - bar.x0 + 1
  10. if bar.inten_0 ne bar.inten_1 then $
  11.     z = byte(bar.inten_0 + findgen(sx) * (bar.inten_1 - bar.inten_0)/sx) $
  12.   else z = replicate(bar.inten_0,sx)
  13.  
  14. for i=bar.y0, bar.y1 do tv,z,bar.x0,i
  15. xyouts, bar.x0, bar.y1+2, strtrim(string(bar.minv,format=bar.nfmt),2),/dev
  16. xyouts, bar.x1, bar.y1+2, strtrim(string(bar.maxv,format=bar.nfmt),2),/dev,align=1.0
  17. xyouts, (bar.x1 + bar.x0)/2, bar.y1+2, bar.title, align=0.5,/dev
  18. plots,[bar.x0, bar.x0,bar.x1,bar.x1,bar.x0],$    ;incribe it
  19.     [bar.y0,bar.y1,bar.y1,bar.y0,bar.y0],/dev
  20. end
  21.  
  22.  
  23. pro c_edit_back
  24. common c_edit_common,nc, nc1, nc2, wxsize, wysize, $
  25.     colors, plot_xs, plot_ys, names, bars
  26.  
  27. on_error,2                      ;Return to caller if an error occurs
  28. ramp = bytscl(indgen(512),top=nc1)
  29. for i=wysize-60,wysize-30 do tv,ramp,wxsize/2-256,i
  30.  
  31. ;define bar structure
  32. a = { slide_bar, x0:0, y0:0, x1:0, y1:0, title:'', minv:0.0, maxv:0.0, $
  33.     inten_0:0, inten_1:0, nfmt:'', str_val:'', value: 1, s: [0.,0.] }
  34.  
  35. bars = replicate(a,4)    ;make 4 of them
  36. bar_wid = wxsize/2.2
  37. bar_x0 = wxsize/4-bar_wid/2
  38.  
  39. bars.x0 = bar_x0
  40. bars.x1 = bar_x0 + bar_wid
  41. bars.y0 = 100*(findgen(4)+1) 
  42. bars.y1 = bars.y0 + 30
  43. bars.title=names
  44. bars.nfmt= ['(f4.0)','(f4.2)','(f4.2)','(f4.0)']
  45. bars.minv = [0,0,0,0]
  46. bars.maxv = [360.,1.0,1.0,nc1]
  47. bars.inten_0 = [nc2,nc2,nc2,0]
  48. bars.inten_1 = [nc2,nc2,nc2,nc1]
  49. for i=0,3 do draw_bar, bars(i)
  50.  
  51. c_labels = ['Red','Green','Blue','Red']
  52. c_align = [0,.5,.5,1]
  53. for i=0,3 do $        ;Label colors for hue
  54.   xyouts,bar_x0 + bar_wid*i/3, bars(0).y0 - !d.y_ch_size,$
  55.     c_labels(i), align = c_align(i), /dev
  56.     
  57. plot_xst = .6
  58. plot_xend = .9
  59. plot_ht = 0.2
  60. yr = [360.,1.,1.]
  61.  
  62. plot_position = fltarr(4,3)
  63. plot_xs = fltarr(2,3)
  64. plot_ys = plot_xs
  65. for i=0,2 do begin
  66.     y = i/3.5+0.1    ;Y of bottom
  67.     plot_position(0,i) = [plot_xst,y, plot_xend, y+plot_ht]
  68.     plot,/noer,colors(*,i),yrange=[0,yr(i)],title=names(i),$
  69.         pos=plot_position(*,i),ystyle=2,xstyle=3, tickl = -0.02
  70.     plot_xs(0,i) = !x.s 
  71.     plot_ys(0,i) = !y.s
  72.     endfor
  73. end
  74.  
  75.  
  76.  
  77. pro interp_colors,pts, npts, colors    ;interpolate colors
  78. ;    pts = array of subscripts of tie points.
  79. ;    npts = # of elements in pts
  80. ;    colors = (n,3) array of colors.  Interpolated between tie points.
  81. ;
  82.     on_error,2                      ;Return to caller if an error occurs
  83.     for i=0,npts-2 do begin    ;interpolate
  84.         i0 = pts(i) & i1 = pts(i+1)
  85.         kc = i1 - i0         ;# of colors to interp+1
  86.         if kc gt 1 then begin
  87.         c = colors(i0,*)
  88.         dc = colors(i1,*) - c    ;delta clockwise
  89.         dc0 = dc(0)        ;hue dist clockwise
  90.         while (dc0 lt (-180.)) do dc0 = dc0 + 360.
  91.         while (dc0 gt 180.) do  dc0 = dc0 - 360.
  92.         dc1 = -dc(0)        ;delta ccw
  93.         while (dc1 lt (-180.)) do dc1 = dc1 + 360.
  94.         while (dc1 gt 180.) do dc1 = dc1 - 360.
  95.         if abs(dc1) lt abs(dc0) then begin    ;Use closest
  96.             dc(0) = dc1
  97.           endif else begin
  98.             dc(0) = dc0
  99.           endelse
  100.         dc = dc / kc
  101.         colors(i0+1,0) = ((findgen(kc-1)+1) # dc) + $  ;Interpolate
  102.                  (replicate(1,kc-1) # c)
  103.         endif        ;kc gt 1
  104.         endfor        ;i loop
  105.     colors(0,0) = (colors(*,0) + 360.) mod 360.    ;wrap the hue
  106. end
  107.  
  108.  
  109.  
  110. pro c_edit, colors_out, hsv = hsv, hls = hls
  111. ;+
  112. ; NAME:
  113. ;    C_EDIT
  114. ;
  115. ; PURPOSE:
  116. ;    Interactive creation of color tables based on the HLS or the HSV color
  117. ;     systems using the mouse and three sliders.  Similar to COLOR_EDIT but
  118. ;    two sliders replace the color wheel.  The sliders allow better control
  119. ;     of HSV colors near 0% saturation, but the interface is less intuitive.
  120. ;
  121. ; CATEGORY:
  122. ;    Color tables.
  123. ;
  124. ; CALLING SEQUENCE:
  125. ;    C_EDIT [,COLORS_OUT] [, HSV = hsv] [, HLS = hls]
  126. ;
  127. ; INPUTS:
  128. ;    None.
  129. ;
  130. ; KEYWORD PARAMETERS:
  131. ;    HLS:  If this keyword is set, use the Hue Lightness Saturation system.
  132. ;    HSV:  If this keyword is set, use the Hue Saturation Value system
  133. ;          (the default).
  134. ;
  135. ; OUTPUTS:
  136. ;    COLORS_OUT:  If supplied, this variable contains the final color
  137. ;    table triples as an array of the form (number_colors, 3).
  138. ;
  139. ; COMMON BLOCKS:
  140. ;    COLORS:  Contains the current RGB color tables.
  141. ;
  142. ; SIDE EFFECTS:
  143. ;    Color tables are modified and values in COLORS common block are
  144. ;    changed.  A temporary window is used.
  145. ;
  146. ; RESTRICTIONS:
  147. ;    Works only with window systems.
  148. ;
  149. ; PROCEDURE:
  150. ;    A window is created with a color bar centered at top and four
  151. ;    sliders along the left side.  The four sliders are labeled:
  152. ;        1) Pixel Value (from 0 to the number of available colors -1 )
  153. ;
  154. ;        2) Value (for HSV) or Lightness (HLS) can have values from
  155. ;           0 to 1.
  156. ;
  157. ;        3) Saturation can have values from 0 to 1.
  158. ;
  159. ;        4) Hue (0 to 360): Red is 0 degrees, green is 120 degrees,
  160. ;           and blue is 240 degrees.
  161. ;
  162. ;    Three graphs on the right show the current values of the three
  163. ;    parameters versus pixel value.
  164. ;
  165. ;    Operation:  The left mouse button is used to mark values on the
  166. ;        sliders.  The middle button is used to erase marked pixel
  167. ;        values (tie points) in the Pixel Value slider.  The right
  168. ;        button updates the color tables and exits the procedure.
  169. ;.
  170. ;    To use: Move the mouse into the slider whose value you want
  171. ;        to change and press the left button to select 
  172. ;        a Value/Lightness, Saturation, or Hue.  Move the mouse
  173. ;        with the left button depressed to interactively alter a color.
  174. ;
  175. ;        When you have created a color, move the mouse to the top
  176. ;        slider and select a pixel value.  The three color parameters
  177. ;        are interpolated between pixel values that have been marked
  178. ;        (called tie points).  Tie points are shown as small vertical
  179. ;        lines beneath the "Pixel Value" slider.  Press the middle
  180. ;        button with the cursor over the Pixel Value slider to delete 
  181. ;        the nearest tie point.
  182. ;
  183. ;    Note that in the HSV system, a Value of 1.0 represents the maximum
  184. ;    brightness of the selected hue.  In the HLS system, a Lightness of 0.5
  185. ;    is the maximum brightness of a chromatic hue, 0.0 is black, and 1.0
  186. ;    is bright white.  In the HLS color space, modeled as a double-ended
  187. ;    cone, the Saturation value has no effect at the extreme ends of the
  188. ;    cone (i.e., lightness = 0 or 1).
  189. ;
  190. ;    You can access the new color tables by declaring the common block
  191. ;    COLORS as follows:
  192. ;       COMMON COLORS, r_orig, g_orig, b_orig, r_curr, g_curr, b_curr
  193. ;
  194. ; MODIFICATION HISTORY:
  195. ;    DMS, July, 1988.
  196. ;       SNG, December, 1990 - For MSDOS only: c_edit does not support 
  197. ;                             resolutions lower than 640x480.
  198. ;-
  199.  
  200. common c_edit_common,nc, nc1, nc2, wxsize, wysize, $
  201.     colors, plot_xs, plot_ys, names, bars
  202.  
  203. common colors, r_orig, g_orig, b_orig, r_curr, g_curr, b_curr
  204.  
  205. on_error,2              ;Return to caller if an error occurs
  206. psave = !p        ;Save !p
  207. nc = !d.table_size    ;# of colors avail
  208. if nc eq 0 then message, 'Device has static color tables, Can''t adjust'
  209.  
  210. device,get_write=old_mask, set_write=255 ;Enable all bits
  211. nc1 = nc -1
  212. nc2 = nc1-1        ;Current color
  213. !p.noclip = 1        ;No clipping
  214. !p.color = nc1        ;Foreground color
  215. !p.font = 0        ;Hdw font
  216. old_window = !d.window    ;Previous window
  217.  
  218. if n_elements(hsv) eq 0 then hsv = 0
  219. if n_elements(hls) eq 0 then hls = 0
  220. if (hsv eq 0) and (hls eq 0) then hsv = 1    ;default system
  221.  
  222. if hsv then begin
  223.     names = ['Hue','Saturation','Value','Pixel Value']
  224. endif else begin
  225.     names = ['Hue','Lightness','Saturation','Pixel Value']
  226. endelse
  227.  
  228. if hls then l = 0.5 else l = 1.0
  229. colors = [[fltarr(nc)],[replicate(l,nc)],[findgen(nc)/nc]]
  230. wxsize = 640
  231. wysize = 600 < !d.y_vsize
  232. if (!d.flags and 256) ne 0 then $
  233.     window,xs=wxsize, ys=wysize, title='Intensity transformation',/free
  234. c_edit_back
  235. tvcrs,.5,.5,/norm
  236. tvlct,colors(0:nc2,0),colors(0:nc2,1),colors(0:nc2,2), hsv = hsv, hls = hls
  237. tvlct,0,0,1,nc1,/hsv        ;last color is white
  238. npts = 2        ;tie points
  239. pts = indgen(nc)    ;x values of tie points
  240. pts(1) = nc1        ;init values
  241. curr = [0,l,1.0,0]    ;Current values
  242. pxl_ind = 0
  243. old_v = 0.
  244. ;        *** Main loop ***
  245.  
  246. next:
  247. tvrdc,x,y,/dev        ;read mouse with wait
  248. next1:
  249. if !err eq 1 then begin
  250. next2:
  251.     for i=0,3 do if (y ge bars(i).y0) and (y le bars(i).y1) then begin
  252.         a = bars(i)        ;Bar struct
  253.         v = (x - a.x0)*(a.maxv - a.minv)/(a.x1 - a.x0) + a.minv
  254.         v = v > a.minv < a.maxv
  255.             ;update text
  256.         xyouts, a.x1+3, a.y0+3 ,a.str_val,col=0,/dev
  257.         bars(i).str_val = strtrim(string(v,format=a.nfmt),2)
  258.         xyouts, a.x1+3, a.y0+3 ,bars(i).str_val,color=nc1,/dev
  259.         curr(i) = v        ;save value
  260.         if i eq 3 then goto, mark_pixel
  261.         tvlct,curr(0),curr(1),curr(2),nc2,hsv=hsv,hls=hls
  262.         tvrdc,x,y,/dev,0
  263.         if !err eq 1 then goto,next2
  264.         endif
  265.     goto,next
  266.  
  267. mark_pixel:        ;Get a hit in the pixel value Y values
  268.     pxl_ind = fix(v)
  269.     x = a.x0 + float(pxl_ind) * (a.x1 - a.x0)/nc1
  270.     plots,[x,x],[a.y0-8,a.y0],/dev
  271.     p = where(pxl_ind eq pts(0:npts-1), n)
  272.     if n eq 0 then begin    ;already there?
  273.         pts(npts) = pxl_ind
  274.         pts(0) = pts(sort(pts(0:npts))) ;re sort
  275.         npts = npts + 1
  276.     endif
  277.     colors(pxl_ind,*) = curr(0:2)
  278. interp_it:
  279.      for i=0,2 do begin    ;erase old plots
  280.         !x.s = plot_xs(*,i)
  281.         !y.s = plot_ys(*,i)
  282.         oplot,colors(*,i),col=0
  283.         endfor
  284.     interp_colors, pts, npts, colors    ;color interp
  285.     tvlct,colors(1:nc2-1,0),colors(1:nc2-1,1), $
  286.         colors(1:nc2-1,2),1, hls = hls, hsv = hsv
  287.     for i=0,2 do begin    ;draw new plots
  288.         !x.s = plot_xs(*,i)
  289.         !y.s = plot_ys(*,i)
  290.         oplot,colors(*,i)
  291.         endfor
  292. endif $        ;!err eq 1
  293. ;        here we delete a point:
  294.  
  295. ;    only remove from bar # 3, the pixel value bar
  296. else if (!err eq 2) and  (y ge bars(3).y0) and (y le bars(3).y1) then  begin
  297.     a = bars(3)        ;Bar struct
  298.     v = (x - a.x0)*(a.maxv - a.minv)/(a.x1 - a.x0) + a.minv
  299.     pxl_ind = fix(v > a.minv < a.maxv) < nc1 > 0 ;pixel value
  300.     j = min(abs(pts(0:npts-1) - pxl_ind),i)    ;get index of closest point
  301.     pxl_ind = pts(i)
  302.     x = a.x0 + float(pxl_ind) * (a.x1 - a.x0)/nc1
  303.     plots,[x,x],[a.y0-8,a.y0-1],/dev,col=0 ;erase tick
  304.          ;never    delete first or last points
  305.     if (i eq 0) or (i eq nc1) then goto, next
  306.     pts = [pts(0:i-1), pts(i+1:*)] ;remove it
  307.     npts = npts - 1
  308.     goto, interp_it
  309. endif $    
  310. else if !err eq 4 then goto,done        ;all done
  311.  
  312. goto,next
  313.  
  314.  
  315. done:    tvlct,r_orig, g_orig, b_orig,/get    ;Read rgb, save in common
  316.     r_curr = r_orig & g_curr = g_orig & b_curr = b_orig
  317.     if n_params() ge 1 then colors_out = [r_orig, g_orig, b_orig]
  318.  
  319.  
  320.         if (!d.flags and 256) ne 0 then begin
  321.         wdelete            ;kill window
  322.       if old_window ge 0 then begin    ;restore window?
  323.         tvcrs,0.5,0.5,/norm    ;show the table
  324.         empty
  325.         tvcrs            ;hide cursor
  326.         endif
  327.         endif
  328. !p = psave
  329. device,set_write=old_mask
  330. end
  331.  
  332.  
  333.  
  334.